<?php
$itemId = Yii::app()->request->getParam('id');
$caption = empty($itemId) ? BUtils::T('dish_create_lbl') : BUtils::T('dish_modify_lbl');
$this->pageTitle = Yii::app()->name . ' - ' . $caption;

$return_url = Yii::app()->createUrl($this->id);
?>
<div class="container prepend-top">
	<?php echo CHtml::button('< ' . BUtils::T('back_btn'), array('class'=>'button', 'onclick'=>"history.go(-1)")); ?>
</div>

<div class="container prepend-top append-bottom relative">
	<div class="span-15">
		<div class="box_white">
		<div class="biz-form">
			<div class="margin">
				<h3><?php echo $caption; ?></h3>
			</div>
<?php
$form = $this->beginWidget('CActiveForm', array(
	'id'=>'dish-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
));
?>

<div class="fieldswrap">
<div class="fields">
	<div class="span-4" style="width:180px;">
		<div class="block">
			<div class="lbl"><?php echo $form->labelEx($model,'url_picture'); ?></div>
			<div class="ipt">
				<?php echo $form->hiddenField($model,'url_picture'); ?>
				<a class="pic"><img id="dish_picture" style="width:150px;" src="<?php echo BUtils::GetPictureUrl($model->url_picture); ?>" alg="dish photo" /></a>
				<br />
<?php $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
	'id' => 'uploadFile',
	'config' => array(
		'buttonName' => BUtils::T('upload_new_picture_eg'),
		'action' => Yii::app()->createUrl('/menu/uploadPicture', array('id'=>$model->id)),
		'allowedExtensions'=>array("jpg","jpeg","gif","png"),
		'sizeLimit'=>8*1024*1024,
		'onComplete' => "js:function(id, fileName, responseJSON){ handleUploaded(responseJSON); }",
	),
)); ?>
			</div>
		</div>
	</div>
	<div class="span-8 last">
		<div class="block">
			<div class="lbl"><?php echo $form->labelEx($model,'name'); ?></div>
			<div class="ipt"><?php echo $form->textField($model,'name', array('class'=>'auto')); ?></div>
			<div class="eg"><?php echo BUtils::T('dish_name_eg'); ?></div>
			<div class="spc"><?php echo $form->error($model,'name'); ?></div>
		</div>
		<div class="block">
			<div class="lbl"><?php echo $form->labelEx($model,'price'); ?></div>
			<div class="ipt">
<?php
$price_array = explode('.', sprintf('%.2f', $model->price));
$priceObject = array(
'price_euro' => ($price_array[0] > 0) ? $price_array[0] : '',
'price_cent' => ($price_array[1] > 0) ? $price_array[1] : '',
);
?>
				<div class="left">
					<span class="symbol">€</span>
					<span class="tidy">
						<input id="dish_price_e" class="dish_price" type="text" maxlength="10" size="10" value="<?php echo $priceObject['price_euro']; ?>" />
						<label for="dish_price_e">Euros</label>
					</span>
					<span class="symbol">,</span>
					<span class="tidy">
						<input id="dish_price_c" class="dish_price" type="text" maxlength="2" size="2" value="<?php echo $priceObject['price_cent']; ?>" />
						<label for="dish_price_c">Cents</label>
					</span>
					<?php echo $form->hiddenField($model, 'price'); ?>
					<div class="clear"></div>
				</div>
				<div class="clear"></div>
			</div>
			<div class="eg"></div>
			<div class="spc"><?php echo $form->error($model,'price'); ?></div>
		</div>
		<div class="block">
			<div class="lbl"><?php echo $form->labelEx($model,'number'); ?></div>
			<div class="ipt"><?php echo $form->textField($model,'number', array('size'=>'3')); ?></div>
		</div>
		<div class="block">
			<?php echo $form->checkBoxList($model, 'featured', array(1=>BUtils::T('dish_featured_lbl'))); ?>
			<div class="spc"></div>
		</div>
	</div>
	<div class="clear"></div>
</div>
<div class="fields">
	<div class="block">
		<div class="lbl"><?php echo $form->labelEx($model,'id_category'); ?></div>
		<div class="ipt tags">
<?php
echo CHtml::textField('dish_category', $selected_category, array('class'=>'auto'));
echo '<div class="dish_categories">';
if (count($dish_categories) > 0) {
	echo '<div class="left" style="width:60px;"><span>' . BUtils::T('dish_category_yours_lbl') . ':</span></div><div style="overflow:hidden;">';
	foreach ($dish_categories as $value) {
		if ($selected_category == $value) {
			echo '<a class="active" href="javascript:;">' . $value . "</a>\n";;
		} else {
			echo '<a href="javascript:;">' . $value . "</a>\n";;
		}
	}
	echo '</div><div class="clear"></div>';
}
$default_categories = BUtils::array_filter_values(BTag::DishCategory(), $dish_categories);
echo '<div class="left" style="width:60px;"><span>' . BUtils::T('dish_category_others_lbl') . ':</span></div><div style="overflow:hidden;">';
foreach ($default_categories as $value) {
	if ($selected_category == $value) {
		echo '<a class="active" href="javascript:;">' . $value . "</a>\n";;
	} else {
		echo '<a href="javascript:;">' . $value . "</a>\n";;
	}
}
echo '</div></div>';
?>
		</div>
		<div class="eg"></div>
		<div class="spc"></div>
	</div>
	<div class="block">
		<div class="lbl"><?php echo $form->labelEx($model,'description'); ?></div>
		<div class="ipt"><?php echo $form->textArea($model,'description', array('class'=>'auto', 'rows'=>'4')); ?></div>
		<div class="ipt">
			<?php echo CHtml::checkBoxList('sensitive_code', explode(',', $model->sensitive_code), BTag::FoodSensitiveCode()); ?>
		</div>
	</div>
</div>
</div><!-- end of .fieldswrap -->

	<div class="margin actions">
		<?php echo CHtml::submitButton(BUtils::T('dish_save_btn'), array('class' => 'button button-huge button-confirm')); ?>
		<?php echo CHtml::button(BUtils::T('cancel_btn'), array('class'=>'button button-huge button-thin', 'onclick'=>'history.go(-1);', 'confirm'=>BUtils::T('cancel_btn_msg'))); ?>
	</div>

<?php $this->endWidget(); ?>
			
		</div><!-- end of <div class="biz-form"> -->
		</div>
	</div>
	
	<div class="span-9 right last" id="sticky-right">
		<div class="box_white">
			<div class="biz-form margin">
				<h3><?php echo BUtils::T('dish_preview_lbl'); ?></h3>
				<div class="tips"><?php echo BUtils::T('menu_how_it_works_eg'); ?></div>
				<div id="menuItem-preview">
<?php
$price = Yii::app()->locale->numberFormatter->formatCurrency($model->price, 'EUR');
$orignal_picture = str_replace('thumb_', '', $model->url_picture);

$sensitive_code_class = empty($model->sensitive_code) ? ' none' : '';
$url_picture_class = empty($model->url_picture) ? ' none' : '';
?>
<div class="dishItem">
	<div class="basic-info relative">
		<div class="name"><?php echo $model->name; ?></div>
		<div class="price"><?php echo $price; ?></div>
	</div>
	<div class="detail-info">
		<div class="picture<?php echo $url_picture_class; ?>">
			<a class="pic popupPicture" href="<?php echo $orignal_picture; ?>"><img src="<?php echo $model->url_picture; ?>" alt="" width="100" /></a>
		</div>
		<div class="sensitive_code<?php echo $sensitive_code_class; ?>" style="margin-top:5px; font-weight:bold;"><?php echo $model->sensitive_code; ?></div>
		<div class="description"><?php echo nl2br(strip_tags($model->description)); ?></div>
		<div class="clear"></div>
	</div>
</div>
				</div>
			</div>
		</div>
	</div>

</div>
<script type="text/javascript">
$(function() {
	//set the preview box as sticky style.
	$('#sticky-right').stickyfloat({duration:0});
	
	//real-time view: dish name
	$('#DishForm_name').keyup(function (event) {
		$('.dishItem .name').html($(this).val());
	});
	
	//change price and real-time view: price euro
	$('.dish_price').change(function(){
		var euro = parseInt($('#dish_price_e').val());
		var cent = parseInt($('#dish_price_c').val());
		if (cent > 0) euro += cent / 100;
		$('#DishForm_price').val(euro);
		$('.dishItem .price').html('€' + euro);
	});
	
	//real-time view: dish description
	$('#DishForm_description').keyup(function (event) {
		$('.dishItem .description').html(nl2br($(this).val()));
	});
	
	//real-time view: food sensitive code
	$('#sensitive_code input:checkbox').change(function () {
		var _selected_string = '';
		$(this).parent().children('input:checked').each(function() {
			_selected_string += $(this).val() + ' ';
		});
		if (_selected_string == '') $('.dishItem .sensitive_code').hide();
		else {
			$('.dishItem .sensitive_code').html(_selected_string).show();
		}
	});
	
	//tags view
	$('.tags a').click(function() {
		$(this).parent().children().removeClass('active');
		$(this).addClass('active');
		$('.tags input').val($(this).text());
	});
	
<?php if (Yii::app()->user->hasFlash('saved')) { ?>
	alert('<?php echo Yii::app()->user->getFlash('saved'); ?>');
<?php } ?>
});
function handleUploaded(respJSON) {
	if (respJSON.url_thumb) {
		var d = new Date();
		var pic = respJSON.url_thumb + '?' + d.getTime();
		$('#dish_picture').attr('src', pic);
		$('#DishForm_url_picture').val(respJSON.url_thumb);
		$('ul.qq-upload-list').delay(1000).html('');
		
		//set picture
		$('.dishItem .picture .popupPicture img').attr('src', respJSON.url_thumb);
		$('.dishItem .picture .popupPicture').attr('href', respJSON.url_thumb.replace('thumb_', '')).fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'overlayShow'	:	true
		});
		$('.dishItem .picture').show();
	}
}
</script>